import 'package:app/common/app_theme.dart';
import 'package:app/common/colors.dart';
import 'package:app/common/tool.dart';
import 'package:app/components/base_scaffold_appbar.dart';
import 'package:app/components/my_webview.dart';
import 'package:app/components/nodata_page.dart';
import 'package:app/pages/document_module/document_model.dart';
import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:get/get.dart';
import 'package:ripple_backdrop_animate_route/ripple_backdrop_animate_route.dart';

import '../../components/custom_footer.dart';
import 'document_controller.dart';
import 'package:flutter_swipe_action_cell/flutter_swipe_action_cell.dart';

class DocumentView extends GetView{

  DocumentController controller = Get.put(DocumentController());


  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      floatingActionButton:

      FloatingActionButton(
        elevation: 0,
        backgroundColor: Colors.transparent,
        onPressed: () {
          RippleBackdropAnimatePage.show(
            context: context,
            child:Container(
              child: Align(
                alignment: Alignment.bottomCenter,
                child: Container(
                  height: 100,
                  // color: Colors.red,
                  child: Row(
                    children: [
                      GestureDetector(
                        onTap: (){
                          Get.back();
                          controller.tapClickCreateDocument();
                        },
                        child: Container(
                          width: 100,
                          height: 100,
                          // color: Colors.grey,
                          alignment: Alignment.center,
                          child: Column(
                            mainAxisAlignment: MainAxisAlignment.center,
                            children: [
                              Image.asset('images/file_folder.png'),
                              SizedBox(height: 5,),
                              Text('创建文件夹')
                            ],
                          ),
                        ),
                      ),
                      // GestureDetector(
                      //   onTap: (){
                      //     Get.back();
                      //     /// 获取上传地址
                      //     controller.requestDataWithUpLoader(6);
                      //     // controller.tapClickCreateDocument();
                      //     // Get.to(MyWebView('title', 'http://49.234.222.178:9097/index?res_model=documents.document&res_id=24&res_field=&company_id=1&create_uid=2&write_uid=2&access_token=430f89381edac608ea51365d4d7403dc&folder_id=0&name=file&offset=10'));
                      //   },
                      //   child: Container(
                      //     width: 100,
                      //     height: 100,
                      //     // color: Colors.grey,
                      //     alignment: Alignment.center,
                      //     child: Column(
                      //       mainAxisAlignment: MainAxisAlignment.center,
                      //       children: [
                      //         Image.asset('images/file_folder.png',),
                      //         SizedBox(height: 5,),
                      //         Text('上传文件')
                      //       ],
                      //     ),
                      //   ),
                      // ),
                    ],
                  ),
                ),
              ),
            ),
            childFade: true,
            duration: 300,
            blurRadius: 20,
            bottomButton: Icon(Icons.clear,color: Colors.grey,),
            bottomHeight: 60,
            bottomButtonRotate: false,
          );
        },
        child: Image.asset('images/float_add_icon.png',width: 45,height: 45,),
      ),

      appBar: BaseAppBar(
        preferredSize: Size(Get.width, AppTool.appBarHEIGHT),
        child: Text('我的文件夹'),
      ),
      body: GetBuilder<DocumentController>(builder: (_){
        return  EasyRefresh.custom(
          emptyWidget: controller.folderArr.isEmpty?NoDataPage():null,
          onRefresh:() => controller.onRefresh(),
          // onLoad: () => controller.onLoad(),
          footer: MaterialFooter1(),
          slivers: [
            SliverList(
              delegate: _mySliverChildBuilderDelegate(),
            ),
          ],
        );
      }),
    );
  }
  _mySliverChildBuilderDelegate() {
    return SliverChildBuilderDelegate(
          (BuildContext context, int index) {
            MyFolderItemModel model = controller.folderArr[index];
            return SwipeActionCell(
              key: ObjectKey(controller.folderArr[index]),
              trailingActions: <SwipeAction>[
                SwipeAction(
                    title: "删除",
                    onTap: (CompletionHandler handler) async {

                      controller.requestDataWithUnlink(document_id: model.id).then((value)async {
                        if(value['errmsg']=='no'){
                          await handler(false);
                        }else{
                          await handler(true);
                        }
                      });
                      // list.removeAt(index);
                      // setState(() {});
                    },
                    color: Colors.red),
                SwipeAction(
                    title: "重命名",
                    onTap: (CompletionHandler handler) async {
                      // controller.dialogRename();

                      BrnMiddleInputDialog(
                          title: '重命名',
                          hintText: '请输入名字',
                          cancelText: '取消',
                          confirmText: '确定',
                          maxLength: 1000,
                          maxLines: 2,
                          barrierDismissible: false,
                          inputEditingController: TextEditingController()..text = '',
                          textInputAction: TextInputAction.done,
                          onConfirm: (value) {
                            controller.requestDataWithReName(name: value,document_id:model.id).then((value) {
                              if(value['errmsg'] == 'ok'){
                                controller.requestDataWithFolder();
                                Get.back();
                              }else{

                              }

                            });
                            // BrnToast.show(value, Get.overlayContext!);
                          },
                          onCancel: () {
                            Get.back();
                            // BrnToast.show("取消", context);
                            // Navigator.pop(context);
                          }).show(Get.overlayContext!);

                      await handler(false);
                      // controller.requestDataWithUnlink(document_id: model.id).then((value)async {
                      //   if(value['errmsg']=='no'){
                      //     await handler(false);
                      //   }else{
                      //     await handler(true);
                      //   }
                      // });
                      // list.removeAt(index);
                      // setState(() {});
                    },
                    color: AppColor.themeColor),
              ],
              child:  GestureDetector(
                onTap:(){
                  controller.tapClickDocument(model.id);
                },
                child:Container(
                  color: Colors.white,
                  child:  Column(
                    children: [
                      Container(
                        padding: EdgeInsets.only(left: 15),
                        height: 60,
                        child:Row(
                          children: [
                            Image.asset('images/file_folder.png'),
                            Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              mainAxisAlignment: MainAxisAlignment.center,
                              children: [
                                Container(
                                  padding: EdgeInsets.only(left: 15,top: 0),
                                  child: Text('${model.name}'),
                                ),
                                Container(
                                  padding: EdgeInsets.only(left: 15,top: 5),
                                  child: Text('${model.data['create_date']}',style: appThemeData.textTheme.subtitle1,),
                                ),
                              ],
                            ),
                          ],
                        ),
                      ),
                      Container(
                        height: 0.5,
                        color: AppColor.lineColor,
                      )
                    ],
                  ),
                ),
              ),
            );
      },
      childCount: controller.folderArr.length,
    );
  }

}